{include file="public/head"}
<div class="main">
    {include file="public/left"}
    <div class="middle">
    {include file="public/header"}
    <div class="find_tab">
        <ul class="find_in">
            <dl class="find_title"><i class="fa fa-address-card" aria-hidden="true"></i><span>{:__('找回密码')}</span></dl>
            <dl class="find_option">
                <dd class="find_sub">
                    <p class="find_tip">{:__('请写手一定要记住自己注册的用户名，一旦用户名遗失，将导致您无法登陆')}</p>
                    <div class="find_pwd find_return"><i class="layui-icon">&#xe65c;</i>  </div>
                    <div class="find_acc">
                        <label nowrap="nowrap">{:__('用户名')}：</label>
                        <input autocomplete="off" id="account" class="layui-input" type="text"/>
                        <span id="account_tip"></span>
                    </div>
                    <div class="find_email find_user">
                        <label nowrap="nowrap">{:__('电子邮箱')}：</label>
                        <input autocomplete="off" id="email" class="layui-input" type="text"/>
                        <span id="email_tip"></span>
                        <button id="send_email" disabled class="layui-btn layui-btn-lg layui-btn-disabled">{:__('发送')}</button>
                    </div>
                    <div class="email_captcha find_user">
                        <label nowrap="nowrap">{:__('邮箱码')}：</label>
                        <input id="captcha" autocomplete="off" class="layui-input" type="text"/>
                        <span id="captcha_tip"></span>
                    </div>
                    <div class="find_pwd">
                        <label nowrap="nowrap">{:__(' 新密码')}：</label>
                        <input autocomplete="off" id="pwd" class="layui-input" type="password"/>
                        <span id="pwd_tip"></span>
                    </div>
                    <div class="find_pwd">
                        <label nowrap="nowrap">{:__('确认密码')}：</label>
                        <input autocomplete="off" id="pwd2" class="layui-input" type="password"/>
                        <span id="pwd2_tip"></span>
                    </div>
                    <!-- <div class="find_captcha">
                        <label nowrap="nowrap">验证码：</label>
                        <input class="layui-input" type="text"/>
                        <iframe class="captcha_img" src="/api/captcha"></iframe>
                    </div>
                    <a href="" class="_clear">看不清，换一张</a> -->
                    <div class="confirm_btn find_user">
                        <button type="button" class="layui-btn layui-btn-lg layui-btn-normal">{:__('下一步')}</button>
                    </div>
                    <div class="log_btn find_pwd">
                        <button type="button" class="layui-btn layui-btn-lg layui-btn-normal">{:__('重置密码')}</button>
                    </div>
                    
                </dd>
            </dl>
            <!-- <dl>
                <hr></hr>
                <b>其他账户登陆</b>
            </dl> -->
        </ul>
        <ul class="no_log">
            <dl class="no_log_service">
                <p class="serve_intro">{:__('注册后您可以享受以下服务')}</p>
                <div class="no_log_get">
                    <p class="get_img money">{:__('赏')}</p>
                    <p class='get_info'>
                        <span>{:__('我要悬赏')}</span>
                        <span>{:__('更低的成本，更多的选择')}</span>
                    </p>
                </div>
                <div class="no_log_get">
                    <p class="get_img getmoney">$</p>
                    <p class='get_info'>
                        <span>{:__('我要赚钱')}</span>
                        <span>{:__('能力变现，有本事你就来')}</span>
                    </p>
                </div>
            </dl>
        </ul>
    </div>
    {include file="public/foot"}
    </div>
    {include file="public/right"}
</div>
{include file="public/footer"}
<script>
    var openflag = 0;
    if($.cookie("think_lang") == 'en-us'){
        $(".find_sub div label").css("width","22%");
        $(".money").css("font-size","17px");
    }
    $(".find_pwd").css("display","none");
    var errorhtl = "<i style='color:rgb(230,46,4);margin-left:5px' class='fa fa-times-circle'></i>";
    var successhtl = "<i style='color:rgb(117,184,64);margin-left:5px' class='fa fa-check-circle-o'></i>";

    $("#account,#email").on('mouseleave',function(){
        var account = $("#account").val();
        var email = $("#email").val();
        if(account == ''){
            $("#account_tip").html("");
            return false;
        }
        if(email == ''){
            $("#email_tip").html("");
            return false;
        }
        var _this = $(this);
        if(account != '' && email != ''){
            $.ajax({
                url:'/api/user/verifyExist',
                type:"post",
                data:{username:account,email:email,merge:1},
                success:function(ret){
                    if(ret['data'] != '' && ret['data'] != undefined && ret['data'] != '' && 
                    ret['data']['exists'] != undefined && ret['data']['exists'] != null){
                        
                        if(ret['data']['exists'] != 1){
                            $("#send_email").attr("disabled",true);
                            $("#send_email").addClass("layui-btn-disabled");
                            $("#send_email").removeClass("layui-btn-primary");
                            var index = layer.tips("{:__('用户名和邮箱不一致')}",_this);
                            
                            $(_this).siblings("span").html(errorhtl);
                            return false;
                        }
                    }
                    $(_this).siblings("span").html(successhtl);
                    var nowid = $(_this).siblings("span").attr("id");

                    if(nowid == 'email_tip'){
                        $("#account_tip").html(successhtl);
                    } else {
                        $("#email_tip").html(successhtl);
                    }
                   if(openflag == 0){
                        $("#send_email").removeAttr("disabled");
                        $("#send_email").removeClass("layui-btn-disabled");
                        $("#send_email").addClass("layui-btn-primary");
                   }
                    
                }
            });
        }
    });
    $("#pwd").blur(function(){
        var pwd = $("#pwd").val();
        if(pwd == ''){
            $("#pwd_tip").html();
            return false;
        }
        var _this = $(this);
        if(pwd.length<6 || pwd.length>20){
            layer.tips("{:__('密码在6-20字符之间')}",_this);
            $("#pwd_tip").html(errorhtl);
            return false;
        }
        if(pwd.indexOf(" ") != -1){
            layer.tips("{:__('密码不能有空格')}",_this);
            return false;
        }
        $("#pwd_tip").html(successhtl);
    });
    $("#pwd2").blur(function(){
        var pwd = $("#pwd").val();
        var pwd2 = $("#pwd2").val();
        var _this = $(this);
        if(pwd2 == ''){
            return false;
        }
        if(pwd != pwd2){
            layer.tips("{:__('密码不一致')}",_this);
            $("#pwd2_tip").html(errorhtl);
            return false;
        }
        $("#pwd2_tip").html(successhtl);
    });
    $(".log_status").click(function(){
        var _this = $(this);
        if(_this.hasClass('layui-form-checked')){
            _this.removeClass('layui-form-checked');
        } else{
            _this.addClass('layui-form-checked');
        }
    });
    $("#send_email").click(function(){
        
        var eventtype = 'resetpwd';
        var email = $("#email").val();
        if(emailCheck(email) === false){
            layer.msg("{:__('电子邮箱格式错误')}",{icon:2});
            return false;
        }
        layer.open({
            title:"{:__('邮箱验证')}",
            tpye:"2",
            btn: ['{:__("确定")}'],
            content:"<div class='_captcha'><iframe class='captcha_img' src='/api/captcha'></iframe><div id='_clear' class='_clear'>{:__('看不清，换一张')}</div><input id='captcha' class='layui-input' type='text'/></div>",
            yes:function(index, layero){
                var captcha = $(layero).find("#captcha").val();
                if(captcha == ''){
                    layer.tips("{:__('验证码不能为空')}",$(layero).find("#captcha"));
                    $(".captcha_img")[0].attributes[1].ownerElement.contentWindow.window.location.reload();
                    return false;
                }
                $(".layui-layer-btn0").css("display","none");
                btntmp = "<button id='no_btn' class='layui-btn layui-btn-disabled'>{:__('确定')}</button>";
                $(".layui-layer-btn").append(btntmp);

                $("#send_email").attr("disabled",true);
                $("#send_email").addClass("layui-btn-disabled");
                openflag = 1;
                $.ajax({
                    url:'/api/EmailCaptcha/send',
                    type:'post',
                    data:{'event':eventtype,'email':email,'captcha':captcha},
                    success:function(ret){
                      
                        if(ret.code != 401 && ret.code != 1){
                            layer.tips(ret.msg,$(layero).find("#captcha"));
                            $(".captcha_img")[0].attributes[1].ownerElement.contentWindow.window.location.reload();
                            $("#no_btn").remove();
                            $(".layui-layer-btn0").css("display","inline-block");
                            $("#send_email").removeClass("layui-btn-disabled");
                            $("#send_email").removeAttr("disabled");
                        } else {
                            layer.msg(ret.msg,{icon:1},function(){
                                var t = 60;
                                var emailintval = setInterval(function(){
                                    $("#send_email").html(t+'s');
                                    t--;
                                    if(t == 0){
                                        clearInterval(emailintval);
                                        $("#send_email").html("{:__('发送')}");
                                        // $("#send_email").removeClass("layui-btn-disabled");
                                        // $("#send_email").removeAttr("disabled");
                                        openflag = 0;
                                    }
                                },1000);
                                layer.close(index);
                            });
                        }
                    },error:function(){
                        layer.msg('{:__("网络异常")}',{icon:2},function(){
                            $("#send_email").removeClass("layui-btn-disabled");
                            $("#send_email").removeAttr("disabled");
                            layer.close(index);
                        });
                    }
                });
            }
        });
        
    });

    $(document).on('click',"#_clear",function(){
        $(".captcha_img")[0].attributes[1].ownerElement.contentWindow.window.location.reload()
    });
    $(".confirm_btn button").click(function(){
        var email = $("#email").val();
        var captcha = $("#captcha").val().replace(/ /g,'');
        var account = $("#account").val();
        if(account == ''){
            layer.msg("{:__('用户名不能为空')}",{icon:2});
            return false;
        }
        if(email == ''){
            layer.msg("{:__('电子邮箱不能为空')}",{icon:2});
            return false;
        }
        if(captcha == ''){
            layer.msg("{:__('邮箱码不能为空')}",{icon:2});
            return false;
        }
        if(captcha.length != 4){
            layer.msg("{:__('邮箱码错误')}",{icon:2});
            return false;
        }
        if(!EamilCodeCheck(captcha)){
            layer.msg("{:__('邮箱码错误')}",{icon:2});
            return false;
        }
        $.ajax({
            url:'/api/user/verifyExist',
            data:{username:account,email:email,email_code:captcha},
            type:'post',
            success:function(ret){
                if(ret['code'] == 1 && ret['data'] != '' && ret['data'] != null && ret['data'] != undefined 
                && ret['data']['exists'] != undefined && ret['data']['exists'] != null){
                    if(ret['data']['exists'] == 1){
                        $(".find_user").css("display","none");
                        $(".find_pwd").css("display",'flex');
                    } else {
                        layer.msg("{:__('邮箱码错误')}",{icon:2});
                    }
                }
            }
        });
    });
    $(document).on('click',".find_return",function(){
        $(".find_user").css("display","flex");
        $(".find_pwd").css("display",'none');
    });
    $(".find_pwd button").click(function(){
        var username = $("#account").val();
        var pwd = $("#pwd").val();
        var pwd2 = $("#pwd2").val();
        var email = $("#email").val();
        var captcha = $("#captcha").val().replace(/ /g,'');
        var resettype = 'email';
        if(username == ''){
            layer.msg("{:__('用户名不能为空')}",{icon:2});
            return false;
        }
        if(pwd == ''){
            layer.msg("{:__('密码不能为空')}",{icon:2});
            return false;
        }
        if(pwd.indexOf(" ") != -1){
            layer.msg("{:__('密码不能有空格')}",{icon:2});
            return false;
        }
        if(pwd.length<6 || pwd.length>20){
            layer.msg("{:__('密码在6-20字符之间')}",{icon:2});
            return false;
        }
        if(pwd != pwd2){
            layer.msg("{:__('密码不一致')}",{icon:2});
            return false;
        }
        if(email == ''){
            layer.msg("{:__('电子邮箱不能为空')}",{icon:2});
            return false;
        }
        if(emailCheck(email) === false){
            layer.msg("{:__('电子邮箱格式错误')}",{icon:2});
            return false;
        }
        if(captcha == ''){
            layer.msg("{:__('邮箱码不能为空')}",{icon:2});
            return false;
        }
        $.ajax({
            url:'/api/user/resetpwd',
            type:'post',
            data:{'username':username,'type':resettype,'newpassword':pwd,'email':email,'captcha':captcha},
            success:function(ret){
                if(ret.code != 401 && ret.code != 1){
                    layer.msg(ret.msg,{icon:2});
                } else {
                    layer.msg(ret.msg,{icon:1},function(){
                        window.location.href='login';
                    });
                }
            }
        });
    });
</script>
